#include "includes.h"

#ifdef USE_PL1167
code uint8_t PrfRegAddrTab[]=
{
  0,1,2,4,5,7,8,9,10,11,12,13,22,23,24,25,26,
  27,28,32,33,34,35,36,37,38,39,40,41,42,43
};

code uint16_t PrfRegTab[]=
{
/*00*/0x6FE0,
/*01*/0x5681,
/*02*/0x6617,
/*04*/0xB1A0,
/*05*/0xC000,
/*07*/0x0030,
/*08*/0x6C91,
/*09*/0x1840,  //0x4010,
/*10*/0x7FFD,
/*11*/0x0008,
/*12*/0x0000,
/*13*/0x48DB,
/*22*/0xC0FF,
/*23*/0x8005,
/*24*/0x0067,
/*25*/0x1659,
/*26*/0x19E0,
/*27*/0x1300,
/*28*/0x1800,
/*32*/0x4800,
/*33*/0x3FC7,
/*34*/0x2000,
/*35*/0x0380,
//Begin Syncword
/*36*/0x4231,
/*37*/0x8575,
/*38*/0x9A0B,
/*39*/0xDECF,
//End syncword
/*40*/0x4401,
/*41*/0xB400,
/*42*/0xFDB0,
/*43*/0x000F
};

//

void PrfInit(void)
{
	uint8_t reinit;
	uint8_t err;
	uint8_t i;
	uint8_t reg;
	union16_t regval;
	
	PrfPortInit();
	DelayMs(10);
  PRF_RSTB_H;
  DelayMs(10);
	
	for(reinit=0;reinit<3;reinit++)
	{
		err = 0;
		for(i=0;i<PRF_INIT_TAB_LEN;i++)
		{
			reg = PrfRegAddrTab[i];
			regval.u16 = PrfRegTab[i];
			PrfWrRegPtr(reg, &regval.u8[0], 2);
			PrfRdRegPtr(reg, &regval.u8[0], 2);
			if(regval.u16!=PrfRegTab[i])
				err = 1;
		}
		if(err==0)
			break;
	}
}

void PrfSetModeCh(uint8_t mode, uint8_t ch)
{
	union16_t regval;
	
	regval.u16 = mode<<7;
	regval.u16 |= ch;
	PrfWrRegPtr(PRF_TX_RX_CH, &regval.u8[0], 2);
}

void PrfWrFifo(uint8_t *ptr, uint8_t len)
{
	PrfWrRegPtr(PRF_FIFO_DAT, ptr, len);
}

void PrfRdFifo(uint8_t *ptr, uint8_t len)
{
	PrfRdRegPtr(PRF_FIFO_DAT|PRF_REG_RD, ptr, len);
}

void PrfClrTxRxPtr(void)
{
	union16_t regval;
	
	regval.u16 = CLR_WR_PTR;
	PrfWrRegPtr(PRF_FIFO_PTR, &regval.u8[0], 2);
}

void PrfSetRx(void)
{
	PrfSetModeCh(PRF_IDLE, Prf.ch);
	PrfClrTxRxPtr();
	PrfSetModeCh(PRF_RX, Prf.ch);
}



#endif	//USE_PL1167_DEMO




#endif	//USE_PL1167
